package middleware

import (
	"context"
	"gdmin/global"
	"gdmin/model"
	"gdmin/utils"
	"github.com/cloudwego/hertz/pkg/app"
	"strings"
)

func MyMiddleware() app.HandlerFunc {
	return func(ctx context.Context, c *app.RequestContext) {
		g := c.Request.Header.Get("Content-Type")

		if !strings.Contains(g, "multipart/form-data") {
			//host := string(c.Host())
			path := c.FullPath()
			method := string(c.Request.Method())
			body := string(c.Request.Body())

			uri := c.GetRequest().URI()
			queryString := string(uri.QueryString())

			userId := utils.GetThreadUserId()
			var log = model.Log{
				Path:        path,
				Method:      method,
				Body:        body,
				QueryString: queryString,
				UserId:      userId,
			}
			if log.Path != "" {

				global.DB.Save(&log)
			}
		}
		c.Next(ctx)

	}
}
